package cc.ug_project.rollcall.Core.Observer

import cc.ug_project.rollcall.Core.ViewModel.IViewModel
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * Created by yeganaaa on 4/3/18.
 */
class Observable<T>(var defaultValue: T) : ReadWriteProperty<Any, T> {
    var get: () -> T = {defaultValue}
    var set: (value: T) -> Unit = {defaultValue  = it}
    var innerSet: (value: T) -> Unit = {}

    override fun getValue(thisRef: Any, property: KProperty<*>): T {
        val value = get()
        if(defaultValue != value) defaultValue = value
        return defaultValue
    }

    override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
        defaultValue = value
        innerSet(value)
        set(value)
    }
}